package com.wnxy.sddfp.system.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import com.wnxy.sddfp.common.model.dto.bank.CardBindDTO;
import com.wnxy.sddfp.common.model.dto.bank.SignatureDTO;
import com.wnxy.sddfp.common.model.dto.user.LoginDto;
import com.wnxy.sddfp.common.model.dto.user.UserRegistDto;
import com.wnxy.sddfp.common.model.vo.user.LoginVo;
import com.wnxy.sddfp.common.result.Result;
import com.wnxy.sddfp.system.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/test")
@Slf4j
@RefreshScope // 配置文件自动更新
public class TestController {

    @GetMapping("/find/{id}")
    @HystrixCommand(fallbackMethod = "errorFallback",commandProperties = {
            // 是否开启断路器
            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED,value = "true"),
            // 请求错误率大于 50% 就启动熔断器
            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE,value = "50"),
            // 默认10秒内访问接口失败达到20次，打开断路器，触发熔断； 为了方便测试，这里修改10秒内错误达到5次触发熔断。
            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD,value = "5"),
            //断路多久以后开始尝试是否恢复，默认5s
            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS,value = "10000"),
    },threadPoolProperties = {
            @HystrixProperty(name = HystrixPropertiesManager.CORE_SIZE,value = "3")
    })
    public Result find(@PathVariable("id")Integer id) {
        if (id < 10) {
            throw new RuntimeException();
        }
        return Result.ok(Thread.currentThread().getName()+"ok");
    }

    // 默认降级方法，返回string
    public Result errorFallback(@PathVariable("id")Integer mid){
        return Result.fail("降级方法");
    }
}






